Partial Public Class App
    Inherits System.Windows.Application

    Public Sub OnApplicationStartup(ByVal sender As Object, ByVal args As StartupEventArgs)
        Dim arg As ScreenSaverArgument = ParseScreenSaverArguments(System.Environment.GetCommandLineArgs())

        'Supporting the ability to show the screen saver and options window.
        Select Case arg
            Case ScreenSaverArgument.ShowScreenSaver
                Dim mainWindow As MainWindow = New MainWindow()
                mainWindow.Show()
            Case ScreenSaverArgument.ShowPreview
            Case ScreenSaverArgument.ShowOptions
                Dim optWindow As OptionsWindow = New OptionsWindow()
                optWindow.Show()
            Case ScreenSaverArgument.ShowDebugMode
            Case ScreenSaverArgument.Invalid
        End Select
    End Sub

    Private Function ParseScreenSaverArguments(ByVal args As String()) As ScreenSaverArgument
        If args Is Nothing Then
            Throw New ArgumentNullException("args")
        End If

        Dim returnArg As ScreenSaverArgument = ScreenSaverArgument.Invalid

        If args.Length > 1 Then
            ' Get the 2 character command line argument
            Dim arg As String = args(1).ToLowerInvariant().Trim().Substring(0, 2)
            Select Case arg
                Case "/c"
                    ' Show the options dialog
                    returnArg = ScreenSaverArgument.ShowOptions
                Case "/p"
                    returnArg = ScreenSaverArgument.ShowPreview
                Case "/s"
                    ' Show screensaver form
                    returnArg = ScreenSaverArgument.ShowScreenSaver
                Case "/d"
                    ' Show screensaver in debug mode
                    returnArg = ScreenSaverArgument.ShowDebugMode
                Case Else
                    returnArg = ScreenSaverArgument.Invalid
                    'MessageBox.Show("Invalid command line argument: " + arg, "Invalid Command Line Argument", MessageBoxButtons.OK, MessageBoxIcon.Error);
            End Select
        Else
            ' If no arguments were passed in, show the screensaver
            returnArg = ScreenSaverArgument.ShowScreenSaver
        End If

        Return returnArg
    End Function

    Friend Enum ScreenSaverArgument
        Invalid
        ShowOptions
        ShowPreview
        ShowScreenSaver
        ShowDebugMode
    End Enum

    ''' <summary>
    ''' Thrown in the event that the Interop.Feeds.dll cannot be located.
    ''' </summary>
    <Serializable()> _
    Public Class NoInteropException
        Inherits System.Exception
        Public Sub New()
        End Sub
        Public Sub New(ByVal message As String)
            MyBase.New(message)
        End Sub
        Public Sub New(ByVal message As String, ByVal inner As Exception)
            MyBase.New(message, inner)
        End Sub
        Protected Sub New(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext)
            MyBase.New(info, context)
        End Sub
    End Class

End Class
